let $dlg = (function(){
  let html = `<div class="dlg-lookup-bg">
  <div class="dlg-lookup">
    <div class="titlebar">
      <p class="title">查找</p>
      <span class="close-btn">×</span>
    </div>
    <div class="main">
      <label>查找内容(N)：</label>
      <input class="lookup-text" type="text" >
      <input class="lookup-next" type="button" value="查找下一个(F)">
      <br>
      <input class="lookup-cancel" type="button" value="取消">
      <div class="demand">
        <div class="case">
          <input type="checkbox" />区分大小写(C)
        </div>
        <fieldset class="direction">
          <legend>方向</legend>
          <input class="up" type="radio" value='up'>向上(U)
          <input class="down" type="radio" value='down'>向下(D)
        </fieldset>
      </div>
    </div>
  </div>
</div>`;
let $body = $('body');
function show(config){
  let $textarea = $('textarea');
  let index = getCursortPosition($textarea.get(0));
   
  $body.append(html);
  let $lookup = $('.dlg-lookup-bg');
  let $close = $('.close-btn');
  let $canc = $('.lookup-cancel');
  let $text = $('.lookup-text');
  let $next = $('.lookup-next');
  //let checkbox = $('input[type="checkbox"]');
  // 关闭对话框
  $close.click(()=>{
    cancel();
  });
  $canc.click(()=>{
    cancel();
  });
  
  // 实时监听textarea
  $textarea.bind('input propertychange',function(){
    console.log($textarea.val());

  })

  // 查找下一个
  $next.click(() => {
    let checkBoxState = $('input[type="checkbox"]').get(0).checked;
    let patStr = $text.val();
    let pattern = checkBoxState===true ? new RegExp(patStr,"g") : new RegExp(patStr, "gi");
    let taStr = $textarea.val().slice(index);
    index+=patStr.length;
    console.log(index);
    let arr = pattern.exec(taStr);
    
    console.log(checkBoxState)
    if(arr !== null){
      let startPos = arr.index;
      setSelectText($textarea.get(0), startPos, startPos+patStr.length);
    }else{
      alert('查询不到'+patStr);
    }
  });



  function cancel() {
    $lookup.remove();
  }
}
  // 获取光标位置
  function getCursortPosition (textDom) {
    var cursorPos = 0;
    if (document.selection) {
        // IE Support
        textDom.focus ();
        var selectRange = document.selection.createRange();
        selectRange.moveStart ('character', -textDom.value.length);
        cursorPos = selectRange.text.length;
    }else if (textDom.selectionStart || textDom.selectionStart == '0') {
        // Firefox support
        cursorPos = textDom.selectionStart;
    }
    return cursorPos;
  }
  // 选中特定范围文本
  function setSelectText(textDom, startPos, endPos) {
    var startPos = parseInt(startPos),
        endPos = parseInt(endPos),
        textLength = textDom.value.length;
    if(textLength){
        if(!startPos){
            startPos = 0;
        }
        if(!endPos){
            endPos = textLength;
        }
        if(startPos > textLength){
            startPos = textLength;
        }
        if(endPos > textLength){
            endPos = textLength;
        }
        if(startPos < 0){
            startPos = textLength + startPos;
        }
        if(endPos < 0){
            endPos = textLength + endPos;
        }
        if(textDom.createTextRange){
            // IE Support
            var range = textDom.createTextRange();
            range.moveStart("character",-textLength);
            range.moveEnd("character",-textLength);
            range.moveStart("character", startPos);
            range.moveEnd("character",endPos);
            range.select();
        }else{
            // Firefox support
            textDom.setSelectionRange(startPos, endPos);
            textDom.focus();
        }
    }
  }

  return  { show };

})();
